package com.leve.control.mapper;

import com.leve.control.domain.Device;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author whongyu
 * @create by 2019/6/13
 */
@Repository
public interface DeviceMapper {

    @Select("<script>" +
            " SELECT b.*,c.title,a.permission,a.cuser_id from ims_levepower_iot_cuser_device a left join ims_levepower_iot_device b on a.device_id=b.id left join ims_levepower_goods c on b.goodsId=c.id " +
            "<where> " +
            "<if test = ' userId != null and userId != \"\" '> and a.cuser_id=#{userId} </if>" +
            "<if test = ' permission != null and permission != -1 '> and a.permission=#{permission} </if>" +
            "</where>" +
            "</script>")
    List<Device> findDevice(String userId, int permission);

    @Select(" select b.id,b.iid,c.title,d.merchname,b.enable from ims_levepower_iot_device b left join ims_levepower_goods c on b.goodsId=c.id left join ims_levepower_merch_user d on b.merchid = d.id where b.id=#{Id} ")
    Device queryDevice(int Id);

    @Insert("insert into ims_levepower_iot_device(`uniacid`, `iid`, `type`, `version`, `enable`, `goodsId`, `merchid`, `state`, `data`) values(#{uniacid}, #{iid}, #{type}, #{version}, #{enable}, #{goodsid}, #{merchid}, #{state}, #{data})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    int add(Device device);

    @Update("<script>" +
            "update ims_levepower_iot_device " +
            "<set>" +
            "<if test=\"uniacid != null\">`uniacid` = #{uniacid}, </if>" +
            "<if test=\"iid != null\">`iid` = #{iid}, </if>" +
            "<if test=\"type != null\">`type` = #{type}, </if>" +
            "<if test=\"version != null\">`version` = #{version}, </if>" +
            "<if test=\"enable != null\">`enable` = #{enable}, </if>" +
            "<if test=\"goodsid != null\">`goodsId` = #{goodsid}, </if>" +
            "<if test=\"merchid != null\">`merchid` = #{merchid}, </if>" +
            "<if test=\"state != null\">`state` = #{state}, </if>" +
            "<if test=\"data != null\">`data` = #{data}</if>" +
            "</set>" +
            " where id = #{id}" +
            "</script>")
    int update(Device device);
}
